<?php
class Admin_model extends Model {
	
	function Admin_model() {
	}
	 
	  
	
	function get_total_videos(){
		$query = "SELECT  COUNT(*) as videos FROM videos ";	
		$query = $this->db->query($query);
		$row = $query->row();
		
		return  $row ? $row->videos : null;
	
	}
	
	function vote_video_up($videoId , $fbId) {
	
		$query = $this->db->query("SELECT * FROM videos_up_votes WHERE videoId = " .$videoId. " AND  fbId = " . $fbId );
		if ($query->num_rows() == 0)
		{
			$this->db->query("INSERT INTO videos_up_votes VALUES( null, '".$videoId."', '".$fbId."', '".now()."' )");
		}
		
		$query = $this->db->query("SELECT COUNT(*) FROM videos_up_votes WHERE videoId = " . $videoId . " GROUP BY videoId;" );
		echo $query->num_rows();
	}
	  
	
	function insert_video($video , $fbId, $name, $description) {
		
		$isFacebookVideo = strrpos($video, "facebook.com/video/") > -1;
		$isYoutubeVideo = strrpos($video, "youtube.com/watch") > -1;
		
		if($isYoutubeVideo)
		{
			$videoId_index =  (strpos($video, "?v=") ?  strpos($video, "?v=") + 3: strpos($video, "&v=") + 3);
			$videoId = substr($video,  $videoId_index , 11) ;
		}
		else if($isFacebookVideo)
		{
			$videoId_index =  (strpos($video, "?v=") ?  strpos($video, "?v=") + 3: strpos($video, "&v=") + 3);
			$videoId = substr($video,  $videoId_index , 15) ;
		}
			
		if(!$isYoutubeVideo && !$isFacebookVideo)
			return false;
			
		$query = $this->db->query("SELECT * FROM videos WHERE video = '" . $videoId . "'");
	 	if ($query->num_rows() > 0)        
	    {
	    	return;
		}
		$data = array(
			'video' => $videoId , 
			'name' => $name,
			'description' => $description,
			'fbId' => $fbId,
			'date' => null,
			'type' => $isFacebookVideo ? 1 : ( $isYoutubeVideo ? 0 : -1)
		);
	
		$this->db->insert('videos', $data);
	
		return true;
	}
	 
	function get_videos_with_top_rated(){
	
		$video_query = "SELECT  C.id,  C.fbId,  C.video, C.type,  COUNT(V.id) as votes
		FROM videos as C LEFT JOIN videos_up_votes as V
		ON C.id = V.videoId WHERE personId = " . $personId .
		" GROUP BY C.id  ORDER BY C.id DESC LIMIT " . VIDEOS_PER_PAGE;
		
		$video_query_rated = "SELECT  C.id,  C.fbId,  C.video, C.type, COUNT(V.id) as votes
		FROM videos as C RIGHT JOIN videos_up_votes as V
		ON C.id = V.videoId WHERE personId = " . $personId .
		" GROUP BY C.id ORDER BY votes DESC LIMIT 3";
		
		$query = $this->db->query($video_query);
		$query_2 = $this->db->query($video_query_rated);
		
		$query_3 = $query->result_array();
		$count = count($query_3);
		for ($i = 0; $i < $count; $i++) 
			$query_3[$i]['thumb'] = $query_3[$i]['type'] == "0" ? "http://i4.ytimg.com/vi/".$query_3[$i]['video']."/default.jpg" : base_url() . 'assets/images/default_video.jpg';
		 
		$query_4 = $query_2->result_array();
		$count = count($query_4);
		for ($i = 0; $i < $count; $i++) 
			$query_4[$i]['thumb'] = $query_4[$i]['type'] == "0" ? "http://i4.ytimg.com/vi/".$query_4[$i]['video']."/default.jpg" : base_url() . 'assets/images/default_video.jpg';
		
		  
		$data_ar = array(
			'videos_count' => count($query_3 ),
			'videos' => $query_3 ,
			'high-rated-videos' => $query_4
		);
		
		if(count($query_3) > 3 && count($query_4) > 1)
		{
			$result = $this->parser->parse('profile_top_videos_template', $data_ar, TRUE);
		}
		else $result = '';
		$result .= $this->parser->parse('profile_videos_template', $data_ar, TRUE);
		return $result;
	
	}
	
	
	function get_person_videos($personId, $offset){
			
		$video_query = "SELECT  C.id,  C.fbId,  C.video, COUNT(V.id) as votes
		FROM videos as C LEFT JOIN videos_up_votes as V
		ON C.id = V.videoId WHERE personId = " . $personId .
		" GROUP BY C.id  ORDER BY C.id DESC LIMIT " . VIDEOS_PER_PAGE . " OFFSET ". $offset;
		 
		
		$query = $this->db->query($video_query); 
		
		$data_ar = array(
			'videos_count' => count($query->result_array() ),
			'videos' => $query->result_array() 
		);
		 
		$result = $this->parser->parse('profile_videos_template', $data_ar, TRUE);
		return $result;
	
	}

}
?>